package com.sentrix.auto.printercenter.domain.event;

import com.sentrix.auto.printercenter.infrastructure.event.EventPublisher;
import com.sentrix.auto.printercenter.infrastructure.event.GuavaEventPublisher;
import com.sentrix.auto.printercenter.infrastructure.util.SpringContextHolder;
import lombok.Getter;

import java.util.ArrayList;
import java.util.List;

@Getter
public class AgentStatusChangedEvent {
    private String agentCode;
    private Boolean isOnline;
    private Long gmtConnect;
    private Long gmtDisconnect;

    private static final List<AgentStatusListener> listeners = new ArrayList<>();

    public AgentStatusChangedEvent(String agentCode, Boolean isOnline, Long gmtConnect, Long gmtDisconnect) {
        this.agentCode = agentCode;
        this.isOnline = isOnline;
        this.gmtConnect = gmtConnect;
        this.gmtDisconnect = gmtDisconnect;
    }

    // 发布事件
    public static void publish(String agentCode, Boolean isOnline, Long gmtConnect, Long gmtDisconnect) {
        AgentStatusChangedEvent event = new AgentStatusChangedEvent(agentCode, isOnline, gmtConnect, gmtDisconnect);
        listeners.forEach(listener -> listener.onStatusChanged(event));

        //测试guava事件
        EventPublisher publisher = SpringContextHolder.getBean(GuavaEventPublisher.class);
        publisher.publishAsync(event);
    }

    // 注册监听器（由基础设施层实现）
    public static void registerListener(AgentStatusListener listener) {
        listeners.add(listener);
    }

    // 监听器接口
    public interface AgentStatusListener {
        void onStatusChanged(AgentStatusChangedEvent event);
    }
}
